package com.yun.zf.admin.service.impl;

import cn.hutool.core.convert.NumberChineseFormatter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfSubOrderMapper;
import com.yun.zf.admin.service.SysRateDiscountService;
import com.yun.zf.admin.service.ZfSubOrderService;
import com.yun.zf.common.admin.dto.order.SubOrderDto;
import com.yun.zf.common.admin.model.order.SubOrderMo;
import com.yun.zf.common.admin.model.pact.RateDiscountMo;
import com.yun.zf.common.app.domain.ZfSubOrder;
import com.yun.zf.common.constant.Constants;
import com.yun.zf.common.mapstruct.admin.OrderMapper;
import com.yun.zf.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同订单分项明细 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-04-25
 */
@Service
public class ZfSubOrderServiceImpl extends ServiceImpl<ZfSubOrderMapper, ZfSubOrder> implements ZfSubOrderService {

    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private SysRateDiscountService rateDiscountService;

    /**
     * 计算业主和客户的分项订单金额，可根据每个城市，小区，以及款项类型进行不同的折扣处理
     * @param type
     * @param finalPrice
     * @param communityId
     * @return
     */
    @Override
    public List<SubOrderMo> computeAmount(String type, BigDecimal finalPrice, Long communityId, Long provCode, Long cityCode) {
        //获取城市折扣比例
        List<RateDiscountMo> rates =  rateDiscountService.getDiscount(provCode,cityCode,type,communityId);
        // 设置折扣默认值
        BigDecimal ownerRate =new BigDecimal("1");
        BigDecimal cusRate =new BigDecimal("1");
        if (rates!=null){
            // 分别获取业主折扣比例与客户折扣比例，如果没设置默认取1
            for (RateDiscountMo rate:rates) {
                if (rate.getMoneyType()==1){
                    ownerRate=rate.getDValue();
                }else if (rate.getMoneyType()==2)
                    cusRate=rate.getDValue();
            }
        }

        String label ="";
        BigDecimal discount= new BigDecimal("1");
        List<SubOrderMo> details = new ArrayList<>();
        if (type.equals("1")) {
            for (int i = 0; i < 2; i++) {
                SubOrderMo orderDetail = new SubOrderMo();
                if (i == 0) {
                    // 1、业主款项
                    orderDetail.setMoneyExplain(Constants.OWNER_EXPLAIN);
                    //2、获取业主佣金比例
                    label = Constants.RENT_OWNER_SCALE;
                    discount =ownerRate;

                } else {
                    label = Constants.RENT_CUSTOMER_SCALE;
                    orderDetail.setMoneyExplain(Constants.CUSTOMER_EXPLAIN);
                    discount=cusRate;
                }
                String scale = dictDataService.getValue("commission", label);
                orderDetail.setAmount(finalPrice.multiply(new BigDecimal(scale)).multiply(discount).setScale(2,BigDecimal.ROUND_HALF_UP));
                orderDetail.setMoneyType(i+1); // 设置款项类型
                orderDetail.setRate(discount); // 设置折扣系数
                details.add(orderDetail);
            }
        } else if (type.equals("2")) {
            for (int i = 0; i < 2; i++) {
                SubOrderMo orderDetail = new SubOrderMo();
                if (i == 0) {
                    // 1、业主款项
                    label = Constants.SALE_OWNER_SCALE;
                    orderDetail.setMoneyExplain(Constants.OWNER_EXPLAIN);
                    discount =ownerRate;
                } else {
                    label = Constants.SALE_CUSTOMER_SCALE;
                    orderDetail.setMoneyExplain(Constants.CUSTOMER_EXPLAIN);
                    discount=cusRate;
                }
                String scale = dictDataService.getValue("commission", label);
                orderDetail.setMoneyType(i+1); // 设置款项类型
                orderDetail.setAmount(finalPrice.multiply(new BigDecimal("10000")).multiply(new BigDecimal(scale)).multiply(discount).setScale(2,BigDecimal.ROUND_HALF_UP));
                orderDetail.setRate(discount); // 设置折扣系数
                details.add(orderDetail);
            }
        }
        return details;
    }
    /**
     * 订单分项明细
     * @return
     */
    @Override
    public Boolean addOrderDetail(Long provCode,Long cityCode,Long orderId,List<SubOrderMo> list) {
        List<ZfSubOrder> details = OrderMapper.INSTANCE.orderDetailMoToDetail(list);
        for (ZfSubOrder orderDetail:details) {
            orderDetail.setProvinceCode(provCode);
            orderDetail.setCityCode(cityCode);
            orderDetail.setOrderId(orderId);
            orderDetail.setOrderStatus("paying");
            orderDetail.setCapital(NumberChineseFormatter.format(orderDetail.getAmount().doubleValue(), true, true));
        }
        return this.saveBatch(details);
    }

    @Override
    public List<SubOrderDto> getDetails(Long provCode, Long cityCode, Long orderId) {
        return baseMapper.getSubOrder(orderId,provCode,cityCode);
    }

    @Override
    public int delSubOrders(Long provCode, Long cityCode, Long orderId) {
        QueryWrapper<ZfSubOrder> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ZfSubOrder::getOrderId,orderId).eq(ZfSubOrder::getCityCode,cityCode).eq(ZfSubOrder::getProvinceCode,provCode);
        return  baseMapper.delete(wrapper);
    }
}
